﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.ASMS.Client.UI.Properties;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
namespace Sunboxsoft.ASMS.Client.UI.Storage
{
    [Obsolete("delete")]
    public partial class CreateSupplierView : UserControl, IView
    {
        public CreateSupplierView()
        {
            InitializeComponent();
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection.AddAction(new ActionNode("save", "保存", OnSaveActionClick))
                .AddAction(new ActionNode("cancel", "取消", OnCancelActionClick));
        }
        public void Active(ViewActiveArgs args)
        {
            //Common.LookUpEditUtil.BindMasterData(this.lupCountry, Domain.Constant.MasterDataCodes.Address, null, Domain.Constant.AddressTypes.Country);
        }
        public void Resume(ViewResumeArgs args)
        {
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("");
        }
        #endregion

        #region Method
        protected override void OnLoad(EventArgs e)
        {
           
            base.OnLoad(e);
        }
        /// <summary>
        /// 数据验证
        /// </summary>
        private bool ValidateData()
        {
            if (string.IsNullOrEmpty(this.txtCode.Text.Trim()))
            {
                UIContext.Current.ViewManager.ShowWarning("请输入供应商编号！");
                return false;
            }
            if (string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                UIContext.Current.ViewManager.ShowWarning("请输入供应商名称！");
                return false;
            }
            if (!string.IsNullOrEmpty(this.txtEmail.Text.Trim())
                && !(new Regex(@"([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,5})+").IsMatch(this.txtEmail.Text.Trim())))
            {
                UIContext.Current.ViewManager.ShowWarning("邮箱地址格式错误！");
                return false;
            }
            return true;
        }
        /// <summary>
        /// 新建供应商
        /// </summary>
        private void CreateSupplier()
        {
            WebRef.ESupplier model = new WebRef.ESupplier();

            model.BusinessId = ModuleContext.Current.BusinessId;
            model.StationId = ModuleContext.Current.StationId;
            model.Code = this.txtCode.Text.Trim();
            model.Name = this.txtName.Text.Trim();
            model.ContactName = this.txtContactName.Text.Trim();
            model.ContactTelephone = this.txtContactTel.Text.Trim();
            model.Telephone = this.txtPhone.Text.Trim();
            model.Country = this.lupCountry.EditValue == null ? "" : this.lupCountry.EditValue.ToString();
            model.Province = this.lupProvince.EditValue == null ? "" : this.lupProvince.EditValue.ToString();
            model.City = this.lupCity.EditValue == null ? "" : this.lupCity.EditValue.ToString();
            model.District = this.lupDistrict.EditValue == null ? "" : this.lupDistrict.EditValue.ToString();
            model.Address = this.txtAddress.Text.Trim();
            model.TaxNumber = this.txtTax.Text.Trim();
            model.BankAccount = this.txtBankAccount.Text.Trim();
            model.QQ = this.txtQQ.Text.Trim();
            model.Fax = this.txtFax.Text.Trim();
            model.Email = this.txtEmail.Text.Trim();
            model.Description = this.txtDesc.Text.Trim();

            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                ServiceResult result = new Service.CreateService().CreateSupplier(model);

                this.BeginInvoke(new Action<ServiceResult>((p) =>
                {
                    this.HideMask();
                    if (p.IsSuccess)
                    {
                        UIContext.Current.ViewManager.CloseView(true, null);
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(p.Message);
                    }
                }), result);
            });
        }
        #endregion

        #region Action
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnSaveActionClick(object obj1, object obj2)
        {
            try
            {
                if (ValidateData())
                {
                    CreateSupplier();
                }
            }
            catch (Exception ex)
            {
                UIContext.Current.ViewManager.ShowError(ex.Message);
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        private void OnCancelActionClick(object obj1, object obj2)
        {
            UIContext.Current.ViewManager.CloseView(false, null);
        }
        /// <summary>
        /// Address 联动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupCountry_EditValueChanged(object sender, EventArgs e)
        {
            //Common.LookUpEditUtil.BindMasterData(this.lupProvince, Domain.Constant.MasterDataCodes.Address, this.lupCountry.EditValue.ToString(), Domain.Constant.AddressTypes.Province);
        }
        /// <summary>
        /// Address 联动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupProvince_EditValueChanged(object sender, EventArgs e)
        {
            //Common.LookUpEditUtil.BindMasterData(this.lupCity, Domain.Constant.MasterDataCodes.Address, this.lupProvince.EditValue.ToString(), Domain.Constant.AddressTypes.City);
        }
        /// <summary>
        /// Address 联动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lupCity_EditValueChanged(object sender, EventArgs e)
        {
            //Common.LookUpEditUtil.BindMasterData(this.lupDistrict, Domain.Constant.MasterDataCodes.Address, this.lupCity.EditValue.ToString(), Domain.Constant.AddressTypes.District);
        }
        #endregion
    }
}
